// typeof的类型缩小

function printId(id: string | number) {
  if (typeof id === 'string') {
    console.log(id.toUpperCase());
  } else {
    console.log(id);
  }
}

// 平等的类型缩小(=== == !== !=/switch)

type Direction = 'left' | 'right' | 'top'
function printDirection(direction:Direction) {
  if (direction === 'left'){
    
  }

  switch(direction){
    case 'left':
      console.log('left');
      break;
    case 'right' :
      console.log('right');
      break
  }
}


//3. instanceof


class Student {
  studying () {
      
  }
}

class Teacher {

}

function who(p:Student|Teacher) {
  if (p instanceof Student){
    p.studying()
  }
}

// in 

type Fish = {
  swimming:() => void   // 函数类型
}

type Dog = {
  running: () => void
}

function walk (animal:Fish|Dog) {
  if ('swimming' in animal){
    animal.swimming()
  }
}

const fish:Fish = {
  swimming(){
    console.log("swimming");
  }
}


